package com.liuchen.boot.config.tomcat;

import org.apache.catalina.connector.Connector;
import org.apache.coyote.http11.Http11NioProtocol;
import org.apache.log4j.Logger;
import org.springframework.boot.context.embedded.tomcat.TomcatConnectorCustomizer;

import java.util.Map;

/**
 * 内置tomcat的参数配置
 */
public class MyTomcatConnectorCustomizer implements TomcatConnectorCustomizer {

    private Map<String, String> rb = null;
    //日志
    private Logger log = Logger.getLogger(getClass().getName());

    public MyTomcatConnectorCustomizer(Map<String, String> rb) {
        this.rb = rb;
    }

    public void customize(Connector connector) {
        Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();
        //设置最大连接数
        int maxConnections = Integer.parseInt(rb.get("tomcatMaxConnections"));
        protocol.setMaxConnections(maxConnections);
        //设置最大线程数
        int maxThreads = Integer.parseInt(rb.get("tomcatMaxThreads"));
        protocol.setMaxThreads(maxThreads);
        int connectionTimeout = Integer.parseInt(rb.get("tomcatConnectionTimeout"));
        protocol.setConnectionTimeout(connectionTimeout);
    }
}